package _2022.hot100._2_两数相加;

/**
 * @author： YHSimon
 * @date： 2022-03-23 14:30
 */
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class Solution {
    //优化
    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        ListNode preHead = new ListNode(-1);
        ListNode pre = preHead;
        int flag = 0;
        while (l1 != null || l2 != null) {
            int n1=l1!=null?l1.val:0;
            int n2=l2!=null?l2.val:0;
            int tmp = n1+n2;
            if (flag!=0) {
                tmp += 1;
            }
            if (tmp > 9) {
                tmp %= 10;
                flag = 1;
            } else {
                flag = 0;
            }
            pre.next = new ListNode(tmp);
            if(l1!=null){
                l1 = l1.next;
            }
            if(l2!=null){
                l2 = l2.next;
            }
            pre = pre.next;
        }

        return preHead.next;
    }


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode preHead = new ListNode(-1);
        ListNode pre = preHead;
        boolean flag = false;
        while (l1 != null && l2 != null) {
            int tmp = l1.val + l2.val;
            if (flag) {
                flag = false;
            }
            pre.next = new ListNode(tmp);
            l1 = l1.next;
            l2 = l2.next;
            pre = pre.next;
        }
        while (l1 != null) {
            int tmp = l1.val;
            if (flag) {
                tmp += 1;
            }
            if (tmp > 9) {
                tmp %= 10;
                flag = true;
            } else {
                flag = false;
            }
            pre.next = new ListNode(tmp);
            l1 = l1.next;
            pre = pre.next;
        }
        while (l2 != null) {
            int tmp = l2.val;
            if (flag) {
                tmp += 1;
            }
            if (tmp > 9) {
                tmp %= 10;
                flag = true;
            } else {
                flag = false;
            }
            pre.next = new ListNode(tmp);
            l2 = l2.next;
            pre = pre.next;
        }
        if (flag) {
            pre.next = new ListNode(1);
        }
        return preHead.next;
    }



    public static void main(String[] args) {
        ListNode l1 = new ListNode(0);
//        ListNode l1 = new ListNode(2);
//        ListNode n1 = new ListNode(4);
//        ListNode n2 = new ListNode(3);
//        l1.next = n1;
//        n1.next = n2;

        ListNode l2 = new ListNode(0);
//        ListNode l2 = new ListNode(5);
//        ListNode n3 = new ListNode(6);
//        ListNode n4 = new ListNode(4);
//        l2.next = n3;
//        n3.next = n4;
        Solution s = new Solution();
        ListNode res = s.addTwoNumbers(l1, l2);
        while(res!=null){
            System.out.print(res.val+" ");
            res=res.next;
        }
    }
}
